package com.ironman.analyze.core.compute;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.analyze.core.compute.base.BaseAverageComputer;
import com.ironman.analyze.core.compute.base.BaseComputer;
import com.ironman.analyze.core.compute.entity.RolWindowData;
import com.ironman.analyze.core.context.TrainingContext;
import com.ironman.analyze.core.handler.RolPostHandler;
import com.ironman.common.entity.RolData;
import com.ironman.common.params.RolParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * 平衡计算
 */
@Slf4j
public class RolAverageComputer extends BaseAverageComputer<RolData> implements BaseComputer<RolData, RolWindowData> {

    private final RolPostHandler rolPostHandler;

    public RolAverageComputer(TrainingContext trainingContext, RolParam rolParam) {
        super(rolParam.getWindowSize(), trainingContext);
        this.rolPostHandler = new RolPostHandler(rolParam);
    }

    @Override
    public void computeWindowData(List<RolData> dataList) {
        for (RolData rolData : dataList) {
            windowData(rolData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double rol) {
                    RolWindowData rolWindowData = new RolWindowData(ts, rol);
                    rolPostHandler.handler(rolWindowData, new Consumer<RolWindowData>() {
                        @Override
                        public void accept(RolWindowData rolWindowData) {
                            syncData(rolWindowData);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void syncData(RolWindowData windowData) {
        getTrainingContext().getRolWindowDataMap().put(windowData.getTs(), windowData);
    }

    public static void main(String[] args) {
        RolAverageComputer rolComputer = new RolAverageComputer(null, null);

        List<RolData> rolDataList = ListUtil.of(
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                new RolData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111)
        );
        rolComputer.computeWindowData(rolDataList);
    }

}
